<template>
  <el-card>
    <div slot="header" style="line-height: 40px;">
      <h1>添加宿舍管理员</h1>
    </div>
    <el-form ref="absencesForm" :rules="rules" label-width="80px" :model="absencesForm">
      <el-form-item label="ID" v-show="false">
        <el-input v-model="absencesForm.id"></el-input>
      </el-form-item>
        <el-form-item label="学生id" prop="stuId">
        <el-input v-model.trim="absencesForm.stuId"></el-input>
      </el-form-item>
      <el-form-item label="缺勤时间" prop="createDate">
        <el-input v-model.trim="absencesForm.createDate"></el-input>
      </el-form-item>
            <el-form-item label="备注" prop="remark">
        <el-input v-model.trim="absencesForm.remark"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import absencesApi from "@/http/absencesApi";

export default {
  data() {
    return {
      absencesForm: {},
      rules: {
        stuId: [
          {required: true, message: '请输入学号', trigger: 'blur'},
        ],
        createDate: [
          {required: true, message: '请输入时间', trigger: 'blur'},
        ],
        remark: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
        ]
      }
    }
  },
  created() {
    this.getEditObj();
  },
  methods: {
    onSubmit() {
      this.$refs['absencesForm'].validate((valid) => {
        if (valid) {
          console.log('submit!', this.absencesForm);
          absencesApi.save(this.absencesForm).then((res) => {
            console.log(res)
            if (res.data.code == 200) {
              this.$router.replace("/absences.html")
            } else {
              this.$message({
                showClose: true,
                message: res.data.msg,
                type: 'error'
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;//阻止表单提交
        }
      });
    },
    onReset() {
      this.$refs["absencesForm"].resetFields();
    },
    getEditObj() {
      let id = this.$route.params.id;
      if (id) {
        absencesApi.edit(id).then(res => {
          if (res.data.code == 200) {
            this.absencesForm = res.data.data;
          }
        })
      }
    }
  }
}
</script>

<style scoped>

</style>